<?php
/**
 * 用户登陆控制器
 */
namespace app\controllers;

use app\models\NormalUser;
use app\modules\admin\models\Config;
use Yii;
use yii\web\Controller;

class LoginController extends Controller{
    public $defaultAction = 'login';

    public function actionLogin(){
        //用户登陆
        $model = new NormalUser();
        $model->scenario = 'login';
        if(Yii::$app->request->isPost){
            $post = Yii::$app->request->post('NormalUser');
            $DM = NormalUser::find()->where(['telephone'=>$post['telephone']])->one();
            if(!is_null($DM) && $DM->status==1 and md5($post['password'].$DM['email'])==$DM->password){
                $session = Yii::$app->session;
                $session['User'] = [
                    'token' => base64_encode($post['telephone']),
                    'nickname' => $DM->nickname,
                    'id' => $DM->id,
                ];
                $this->redirect(['user/index']);
            }
            else{
                echo "<script>alert('密码错误或未注册！请重试或进行注册');location.href='index.php?r=login'</script>";
            }
        }
        return $this->renderPartial('/user/login',['model'=>$model]);
    }

    public function actionRegister(){
        //用户注册

        //注册页面只需填写部分信息，注册成功后邮箱验证自动
        //跳转到 modify  页面 完善个人信息

        $model = new NormalUser();
        $model->scenario = 'register';
        $data = Yii::$app->request;
        if($data->isPost){
            $post = $data->post('NormalUser');
            $DM = NormalUser::find()->where(['email'=>$post['email']])->one();
            if(is_null($DM)){    //数据库中没有该用户才允许注册
                $DM = new NormalUser();
                $DM->email = $post['email'];
                $DM->telephone = $post['telephone'];
                $DM->password = md5($post['password'].$post['email']);
                $DM->status = 1;
                if($DM->save(false)){
                    //注册后进入登录界面
                    echo "<script>alert('注册成功！');location.href='index.php?r=login/login'</script>";
                }else{
                    echo "<script>alert('注册失败请重试');location.href='index.php?r=login/register'</script>";
                }
            }
        }
        return $this->renderPartial('/user/register',['model'=>$model]);
    }

    public function actionForget(){
        //用户密码重置
        $model = new NormalUser();
        $model->scenario = 'forget';
        if(Yii::$app->request->isPost){
            $post = Yii::$app->request->post('NormalUser');
            $DM = NormalUser::find()->where(['email'=>$post['email']])->one();
            if(!is_null($DM)){
                $DM->nickname = $post['nickname'];
                $DM->email = $post['email'];
                $DM->password = md5($post['password'].$post['email']);
                $DM->status = 0;
                $DM->save(false);
                $this->SendEmail($post['email']);    //发送邮件
                return $this->renderPartial('/user/login',['model'=>$model]);
            }else
                echo "<script>alert('尚无该用户信息，请前往注册');location.href='index.php?r=login/register'</script>";
        }
        return $this->renderPartial('/user/forget',['model'=>$model]);
    }

    public function SendEmail($email)
    {    //邮箱验证

        //抽取邮箱验证地址
        $url = Config::findOne(1);
        $url = $url->web_url;

        $token = crypt($email, md5($email));
        $validate = base64_encode($email);
        //邮箱发送
        $mail= Yii::$app->mailer->compose();
        $mail->setTo($email);
        $mail->setSubject("激活邮箱");
        //发布可以带html标签的文本
        $mail->setHtmlBody("爱未来兼职平台注册用户激活链接 <a href='".$url."/index.php?r=login/live&token=".$token."&validate=".$validate."'>点击链接激活邮箱</a>");
        if($mail->send())
            echo "<script>alert('邮件已成功发送至你的邮箱，请注意查收！');location.href='index.php?r=login/login'</script>";
        else
            echo "<script>alert('邮件发送失败，请重试！');location.href='index.php?r=login/login'</script>";
        die();
    }


    public function actionLive()
    {
        $email=base64_decode(Yii::$app->request->get('validate'));
        $token=Yii::$app->request->get('token');
        $token_check=crypt($email,md5($email));
        if($token_check==$token)
        {
            $DM = NormalUser::find()->where(['email'=>$email])->one();
            $DM->status = 1;
            $DM = $DM->save(false);
            if($DM)
            {
                echo "<script>alert('激活成功，请登录');location.href='index.php?r=login/login'</script>";
            }
            else
            {
                echo "<script>alert('激活失败,请重试');location.href='index.php?r=login/forget'</script>";
            }
        }
        else
        {
            echo "<script>alert('参数错误,请重新激活');location.href='index.php?r=login/forget'</script>";
        }
    }

}
